var api = require('../../utils/api.js');
var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    zf: 1,
    zj: 0,
    see:false,
    goods: [],
    goodtit: '',
    show:0,
    'region':["四川省", "成都市", "温江区"]
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  onLoad(options) {
    this.setData({
      id: options.id
    })

    var that = this;
    api.get({
      url: 'wxapp/user/getorderbyid',
      data: {
        id: options.id
      },
      success: data => {
        console.log(data);
        that.setData({
          goods: data.data.list
        })
      }
    });

  },

  formSubmit(e) {
    console.log(e.detail.value)

  


    var that = this;
    var id = this.data.id;
    var data = e.detail.value;

    var see=this.data.see;

    if(!see){
        wx.showToast({
          title: '请阅读隐形协议',
          icon:'none'
        })

        return false;
    }

    data.gid = id;
    data.goods = this.data.goodtit;

    console.log(data)
    
    var qy=data['qy'];

   
    data.address = qy[0]+qy[1]+qy[2]+data.address;


    api.get({
      url: 'wxapp/user/addorder',
      data: data,
      success: data => {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })

        if (data.code == 1) {

            wx.requestSubscribeMessage({
                tmplIds: ['-5D9CBr3OyEKA_1Nbgs27mZbxDxfhnOmTgW6gKIE2mw','DprgOzUXL3IQ-3vY8RrO98pEcrAgKlbWV2TofwK9Pe4','x-LbrZ6TSnXoRzyTF76AU6zKKhkJY3Ke6Hsc68P1gHk'],
                success (res) {
                    setTimeout(function () {
                        wx.navigateTo({
                          url: '/pages/order/index',
                        })
                      }, 1500)
                 },
                 fail(ret){
                     console.log(ret)
                 }
              })


    
        }
      }
    });

  },

  onShow() {

  

    var that = this;

    api.get({
      url: 'wxapp/index/test',
      data: {},
      success: data => {
          console.log(data);

          that.setData({
            show: data.data.show
          })
      }
    })

    api.get({
      url: 'wxapp/user/getUserInfo',
      data: {},
      success: data => {
        console.log(data);
        var user = data.data;
        that.setData({
          user: user
        })

        if (!user.agentid) {

          wx.showModal({
            title: '提示',
            content: '你还未进行认证!',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/rz/index',
                })
              }
            }
          })
        }

        if (!user.team_id) {

            wx.showModal({
              title: '提示',
              content: '你还未进行绑定!',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/index/select',
                  })
                }
              }
            })
          }


      }
    });

  },

  bindPickerChange(ret) {
    console.log(ret);
    var index = ret.detail.value;
    var goods = this.data.goods;



    this.setData({
      index: ret.detail.value,
      goodtit: goods[index]
    })


  },



  radioChangezj(ret) {
    console.log(ret);
    this.setData({
      zj: ret.detail.value
    })
  },

  switch2Change(ret){
      console.log(ret);

      this.setData({
          see:ret.detail.value
      })

  },
  seexieyi(){
      wx.navigateTo({
        url: '/pages/index/xieyi',
      })
  }

})